package com.rayo.accesscontrollercopy.module.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.rayo.accesscontrollercopy.R;
import com.rayo.accesscontrollercopy.config.GlobalVariables;
import com.rayo.accesscontrollercopy.model.db.bean.LogData;
import com.rayo.accesscontrollercopy.utils.KeyEventTypeUtils;
import com.rayo.accesscontrollercopy.utils.TimeFormatUtil;

import java.util.List;


/**
 * Created by lyc-pc on 17/2/23.
 */

public class LogAdapter extends RecyclerView.Adapter<LogAdapter.LogViewHolder> {
    private Context mContext;
    private List<LogData> mList;

    public LogAdapter(Context context, List<LogData> list) {
        mContext = context;
        mList = list;
    }

    @Override
    public LogViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new LogViewHolder(LayoutInflater.from(mContext).inflate(R.layout.adapter_log, parent, false));
    }

    @Override
    public void onBindViewHolder(LogViewHolder holder, int position) {
        final int pos = holder.getLayoutPosition();
        LogData logData = mList.get(position);
        if (TextUtils.isEmpty(logData.getUserlastname()) && TextUtils.isEmpty(logData.getUserfirstname())) {
            if(TextUtils.isEmpty(logData.getLockname()))
                holder.mTvAdapterLogMsg.setText(mContext.getString(R.string.unknown)+"("+logData.getUserid()+")" + "  " + mContext.getString(R.string.open) + " " + mContext.getString(R.string.unknown)+ ""+"("+logData.getLockid()+""+")");
            else
                holder.mTvAdapterLogMsg.setText(mContext.getString(R.string.unknown)+"("+logData.getUserid()+")" + "  " + mContext.getString(R.string.open) + " " + logData.getLockname()+ "");


        } else {
            if (GlobalVariables.getInstance().getmSystemData().getNameFormat() == 1) {
                if(TextUtils.isEmpty(logData.getLockname())) {
                    holder.mTvAdapterLogMsg.setText(logData.getUserlastname() + " " + logData.getUserfirstname() + "  " + mContext.getString(R.string.open) + " " +mContext.getString(R.string.unknown) + ""+ ""+"("+logData.getLockid()+""+")");
                }
                else {
                    holder.mTvAdapterLogMsg.setText(logData.getUserlastname() + " " + logData.getUserfirstname() + "  " + mContext.getString(R.string.open) + " " + logData.getLockname() + "");
                }
            } else {
                if(TextUtils.isEmpty(logData.getLockname())) {
                    holder.mTvAdapterLogMsg.setText(logData.getUserfirstname() + " " + logData.getUserlastname() + "  " + mContext.getString(R.string.open) + " " +mContext.getString(R.string.unknown) + ""+ ""+"("+logData.getLockid()+""+")");
                }
                else {
                    holder.mTvAdapterLogMsg.setText(logData.getUserfirstname() + " " + logData.getUserlastname() + "  " + mContext.getString(R.string.open) + " " + logData.getLockname() + "");
                }
            }


        }
        holder.mTvAdapterLogResult.setText(mContext.getString(R.string.result) + ": " + KeyEventTypeUtils.getResult(mContext, ((byte) logData.getType())));

        holder.mTvAdapterLogTime.setText(mContext.getString(R.string.time) + ": " + TimeFormatUtil.showDateTime(mContext,logData.getTime()));

    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class LogViewHolder extends RecyclerView.ViewHolder {

        TextView mTvAdapterLogMsg;

        TextView mTvAdapterLogResult;

        TextView mTvAdapterLogTime;

        public LogViewHolder(View itemView) {
            super(itemView);
            mTvAdapterLogMsg = (TextView)itemView.findViewById(R.id.tv_adapter_log_msg);
            mTvAdapterLogResult = (TextView)itemView.findViewById(R.id.tv_adapter_log_result);
            mTvAdapterLogTime = (TextView)itemView.findViewById(R.id.tv_adapter_log_time);
        }
    }
}
